package com.wengyingjian.https;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hzwengyingjian on 17/1/22.
 */
public class Main {

    public static void main(String[] args)
            throws IOException, NoSuchProviderException, NoSuchAlgorithmException, KeyManagementException {
        TrustManager[] tm = { new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } };
        SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
        sslContext.init(null, tm, new java.security.SecureRandom());
        // 从上述SSLContext对象中得到SSLSocketFactory对象
        SSLSocketFactory ssf = sslContext.getSocketFactory();

        String requestUrl = "https://login.taobao.com/aso/tgs?domain=alimama&sign_account=78ddbd8eafb74d4573fbbac5677bfa45&service=user_on_taobao&target=687474703A2F2F7777772E616C696D616D612E636F6D2F696E6465782E68746D";
        // String requestUrl =
        // "https://login.taobao.com/aso/tgs?domain=alimama&sign_account=78ddbd8eafb74d4573fbbac5677bfa45&service=user_on_taobao&target=687474703A2F2F7777772E616C696D616D612E636F6D2F696E6465782E68746D";
        URL url = new URL(requestUrl);
        HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
        httpUrlConn.setSSLSocketFactory(ssf);
//        httpUrlConn.setInstanceFollowRedirects(false);

        Map<String, String> map = getCookie();
        for (Map.Entry<String, String> e : map.entrySet()) {
            httpUrlConn.setRequestProperty(e.getKey(), e.getValue());
        }
        System.out.println("url="+requestUrl);
        System.out.println("cookieMap="+map);

        // httpUrlConn.setDoOutput(true);
        // httpUrlConn.setDoInput(true);
        // httpUrlConn.setUseCaches(false);
        httpUrlConn.connect();
        System.out.println("headers=" + httpUrlConn.getHeaderField("Location"));
        System.out.println("headers=" + httpUrlConn.getHeaderFields());
        String url2 = httpUrlConn.getHeaderField("Location");

    }

    public static Map getCookie() {
//        String headerStr = "Cookie: thw=cn; cna=vT8DEV9fBn0CAXJxxYPndJaI; munb=1126400639; v=0; linezing_session=z4oi78cBjzYlXcih2lbkI6gM_1485009030955Vjq9_1; uc2=wuf=https%3A%2F%2Ftrade.tmall.com%2Fdetail%2ForderDetail.htm%3Fbiz_order_id%3D3005257425353906%26forward_action%3D; _tb_token_=f33ba340134e3; _umdata=0823A424438F76AB09665CC0588377B5D3600910B699A14574A872ACA37D32E614C496F24F449463CD43AD3E795C914CFACB4FA34E3AAA55D18DBBC69DF31883; l=AuLiVuErWIGG2E4/vXLmQNaysmJEM-ZN; isg=AqSkE_1KoJLcjNTe5aj8xuVVdaT3uMinTj-9cr7FMG8yaUQz5k2YN9rLX3YL; uc1=cookie14=UoW%2FWNO2R9WRww%3D%3D&lng=zh_CN&cookie16=U%2BGCWk%2F74Mx5tgzv3dWpnhjPaQ%3D%3D&existShop=true&cookie21=Vq8l%2BKCLiv0MyZ1zjQnMQw%3D%3D&tag=7&cookie15=UtASsssmOIJ0bQ%3D%3D&pas=0; uc3=sg2=BxpRSReStIeCmWep1j1R677YRtWK6FNc0SWEmUKfmG4%3D&nk2=EqefhWnclW8Zvw%3D%3D&id2=UoCLEaUbCeipTw%3D%3D&vt3=F8dARHXUU5oml3j%2FJf4%3D&lg2=WqG3DMC9VAQiUQ%3D%3D; existShop=MTQ4NTE3MDM5OA%3D%3D; lid=q291611265; lgc=q291611265; tracknick=q291611265; cookie2=14e70455c63599a6db231ca5c959931f; sg=595; mt=np=&ci=0_1; cookie1=VAn%2BSrhUnehPiB3itMA4s%2FOhFo62QHTUdtDDiK%2BaJB4%3D; unb=1126400639; skt=8b0cc7185209b71d; t=e7e53a3daff516f5c20baebac84ec5a8; _cc_=Vq8l%2BKCLiw%3D%3D; tg=0; _l_g_=Ug%3D%3D; _nk_=q291611265; cookie17=UoCLEaUbCeipTw%3D%3D; lc=Vy%2BdFYBitfWv3QlFnMe3";
        String headerStr="Cookie: thw=cn; cna=vT8DEV9fBn0CAXJxxYPndJaI; munb=1126400639; uc2=wuf=https%3A%2F%2Ftrade.tmall.com%2Fdetail%2ForderDetail.htm%3Fbiz_order_id%3D3005257425353906%26forward_action%3D; v=0; _tb_token_=ebebe3b1fe37e; _umdata=0823A424438F76AB09665CC0588377B5D3600910B699A14574A872ACA37D32E614C496F24F449463CD43AD3E795C914CFACB4FA34E3AAA55D18DBBC69DF31883; l=AhoavApBgKkOQAbntZouGFSr6so8S54l; isg=AmBg39nrLCZqkZAiAXzA8rnJMWhT7EQzIhN5rtpxLHsO1QD_gnkUwzbnG8ov; uc1=cookie14=UoW%2FWNO2QjckZw%3D%3D&lng=zh_CN&cookie16=U%2BGCWk%2F74Mx5tgzv3dWpnhjPaQ%3D%3D&existShop=true&cookie21=U%2BGCWk%2F7owY3j65jYmjW9Q%3D%3D&tag=7&cookie15=W5iHLLyFOGW7aA%3D%3D&pas=0; uc3=sg2=BxpRSReStIeCmWep1j1R677YRtWK6FNc0SWEmUKfmG4%3D&nk2=EqefhWnclW8Zvw%3D%3D&id2=UoCLEaUbCeipTw%3D%3D&vt3=F8dARHXUU5%2FQRBTMdsE%3D&lg2=UtASsssmOIJ0bQ%3D%3D; existShop=MTQ4NTE3NTk4MA%3D%3D; lid=q291611265; lgc=q291611265; tracknick=q291611265; cookie2=1fa302bfd26d554f542bdd36765dfa55; sg=595; mt=np=&ci=0_1; cookie1=VAn%2BSrhUnehPiB3itMA4s%2FOhFo62QHTUdtDDiK%2BaJB4%3D; unb=1126400639; skt=bf3834949189cf8d; t=e7e53a3daff516f5c20baebac84ec5a8; _cc_=V32FPkk%2Fhw%3D%3D; tg=0; _l_g_=Ug%3D%3D; _nk_=q291611265; cookie17=UoCLEaUbCeipTw%3D%3D; lc=Vy%2BdFYBitfWv3QlFnMe3";
        String[] lines = headerStr.split("\n");
        Map<String, String> headers = new HashMap<String, String>();
        // headers.put(":authority", "login.taobao.com");
        // headers.put(":method", "GET");
        // headers.put(":path",
        // "/aso/tgs?domain=alimama&sign_account=78ddbd8eafb74d4573fbbac5677bfa45&service=user_on_taobao&target=687474703A2F2F7777772E616C696D616D612E636F6D2F696E6465782E68746D");
        // headers.put(":scheme", "https");

        for (String line : lines) {
            int index = line.indexOf(":");
            headers.put(line.substring(0, index), line.substring(index + 1, line.length()));
        }
        return headers;
    }

}
